package com.bestcem.xm.ticket.grpc.client;

import com.bestcem.bp.grpc.v1.base.Code;
import com.bestcem.bp.grpc.v1.base.Page;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.member.grpc.v1.common.Member;
import com.bestcem.xm.member.grpc.v1.enums.MemberTypeEnum;
import com.bestcem.xm.member.grpc.v1.services.*;
import com.bestcem.xm.ticket.controller.param.PageParam;
import com.bestcem.xm.ticket.grpc.client.dto.survey.MemberNodeDTO;
import com.bestcem.xm.ticket.grpc.client.dto.user.MemberDTO;
import com.bestcem.xm.ticket.service.dto.PageDTO;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 联系人模块 手动编写的client 方法
 *
 * @author gy.liu
 * @version 1.0
 * @date 2021/8/30 16:51
 **/
@Slf4j
@Service
public class TicketMemberGrpcService {

    @GrpcClient(GrpcClientNames.DS_MEMBER)
    private MemberServiceGrpc.MemberServiceBlockingStub memberServiceBlockingStub;

    @GrpcClient(GrpcClientNames.DS_MEMBER)
    private TicketServiceGrpc.TicketServiceBlockingStub ticketServiceBlockingStub;


    /**
     * 根据机构号获取联系人列表
     *
     * @param orgId
     * @return String
     * @author gy.liu
     * @date 2021/9/8
     */
    @Nonnull
    public PageDTO<MemberDTO> listMembersByOrgId(String orgId, PageParam pageParam) {
        ListMembersByOrgIdRequest.Builder builder = ListMembersByOrgIdRequest.newBuilder();
        builder.setOrgId(orgId);
        builder.setPage(Page.newBuilder().setPage(pageParam.getPage()).setSize(pageParam.getSize()));
        builder.setMemberTypeValue(MemberTypeEnum.MemberType.INDIVIDUAL_VALUE);

        try {
            ListMembersByOrgIdResponse response = ticketServiceBlockingStub.listMembersByOrgId(builder.build());
            if (Objects.isNull(response) || Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] MEMBER listMembersByOrgId {} Error; ErrorMsg: {}", orgId, response);
                return new PageDTO<>(pageParam.getPage(), new ArrayList<>(), 0L);
            }

            List<Member> members = response.getMembersList();
            List<MemberDTO> memberDTOS = this.memberCovert(members);
            return new PageDTO<>(pageParam.getPage(), memberDTOS, (long) response.getTotal());
        } catch (Exception e) {
            log.error("[Ticket] MEMBER listMembersByOrgId {} Error;", orgId, e);
            return new PageDTO<>(pageParam.getPage(), new ArrayList<>(), 0L);
        }
    }

    /**
     * 方法描述
     *
     * @param id
     * @author gy.liu
     * @date 2021/9/9
     */
    @Nullable
    public MemberNodeDTO getMemberById(String id) {
        GetMemberRequest.Builder builder = GetMemberRequest.newBuilder();
        builder.setId(id);
        try {
            GetMemberResponse response = memberServiceBlockingStub.getMember(builder.build());
            if (Objects.isNull(response) || Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] MEMBER listMembersByOrgId {} Error; ErrorMsg: {}", id, response);
                return null;
            }
            Member member = response.getMember();
            return memberCovertById(member);
        } catch (Exception e) {
            log.error("[Ticket] MEMBER listMembersByOrgId {} Error; ErrorMsg: {}", id, e.getMessage(), e);
            return null;
        }
    }

    public List<MemberDTO> memberCovert(List<Member> members) {
        if (!CollectionUtils.isEmpty(members)) {
            List<MemberDTO> memberDTOS = new ArrayList<>();
            members.forEach(member -> {
                MemberDTO memberDTO = new MemberDTO();
                memberDTO.setId(member.getId());
                memberDTO.setMail(member.getEmail());
                memberDTO.setName(member.getName());
                memberDTO.setPhone(member.getPhone());
                memberDTOS.add(memberDTO);
            });
            return memberDTOS;
        }
        return new ArrayList<>(0);
    }

    public MemberNodeDTO memberCovertById(Member member) {
        if (Objects.nonNull(member)) {
            MemberNodeDTO memberDTO = new MemberNodeDTO();
            memberDTO.setName(member.getName());
            memberDTO.setGender(member.getGender());
            memberDTO.setPhone(member.getPhone());
            memberDTO.setEmail(member.getEmail());
            return memberDTO;
        }
        return null;
    }
}
